<?php
class Pagination{
    public static function page($current, $total, $per_page, $url = '') {
        $pages = ceil($total/$per_page);
        $str = '';
        if ($pages <= 6) {
            for($i = 1; $i<=$pages; $i++) {
                if ($i != $current) $str .= "<li><a href='?p={$i}'>{$i}</a></li>";
                else $str .= "<li class='active'><a href='?p={$i}'>{$i}</a></li>";
            }
        } elseif ($pages >= 7 ) {
            if($current >= 5)$str = "<li><a href='?p=1'>1</a></li><li><a href='?p=1'>&lt;&lt;</a></li></li>";
            for($i = $current-3; $i <=$current+3; $i++) {
                if ($i <= 0)continue;
                if ($i > $pages) continue;
                if ($i != $current) $str .= "<li><a href='?p={$i}'>{$i}</a></li>";
                else $str .= "<li class='active'><a href='?p={$i}'>{$i}</a></li>";
            }
            if ($pages - ($current + 2) > 1) $str .= "<li><a href='?p={$pages}'>&gt;&gt;</a></li><li><a href='?p={$pages}'>{$pages}</a></li>";
        } else {
            for($i = $current-3; $i<$current+3; $i++) {
                if ($i <= 0)continue;
                if ($i > $pages) continue;
                if ($i != $current) $str .= "<li><a href='?p={$i}'>{$i}</a></li>";
                else $str .= "<li class='active'><a href='?p={$i}'>{$i}</a></li>";
            }
        }
        if (strstr($url, '?')) {
            $str = str_replace('?p=', $url . '&p=', $str);
        }
        return $str;
    }
}
